<%@ page import="com.atlassian.jira.portal.PortletConfiguration,
                 com.atlassian.jira.web.portlet.bean.PortletRenderer,
                 com.atlassian.util.profiling.UtilTimerStack,
                 webwork.action.CoreActionContext"%>
<%@ page import="com.atlassian.jira.ComponentManager" %>
<%@ taglib uri="webwork" prefix="webwork" %>
<%@ taglib uri="sitemesh-page" prefix="page" %>
<webwork:action name="'util.UserAction'" id="user" />

<html>
	<head>
		<title>
            <webwork:if test="currentPortalPage/pageName == 'dashboard'"><webwork:text name="'common.concepts.dashboard'" /></webwork:if>
            <webwork:else><webwork:property value="currentPortalPage/pageName" /></webwork:else>
        </title>
    </head>
	<body>

    <%-- this javascript include is for the projects.jsp portal, but we only want to include it once, so we do it here --%>
    <script language="JavaScript" type="text/javascript" src="<%= ComponentManager.getInstance().getWebResourceManager().getStaticResourcePrefix() %>/includes/js/cookieUtil-min.js"></script>
	
    <table cellpadding=2 cellspacing=0 border=0 width=100% align=center style="border-bottom: 1px solid #bbb;">
    <tr>
        <webwork:if test="/dashboardPages/size > 1">
        <webwork:iterator value="/dashboardPages">
            <webwork:if test="/currentPageId == ./id">
                <td bgcolor=bbbbbb width="1%" nowrap align=center>
                    &nbsp;<font color=ffffff><b><webwork:if test="pageName == 'dashboard'"><webwork:text name="'common.concepts.dashboard'" /></webwork:if><webwork:else><webwork:property value="pageName" /></webwork:else></b></font>&nbsp;
                </td>
            </webwork:if>
            <webwork:else>
                <td width="1%" nowrap align=center>
                   		&nbsp;<b><a href="Dashboard.jspa?selectPageId=<webwork:property value="./id" />"><webwork:if test="pageName == 'dashboard'"><webwork:text name="'common.concepts.dashboard'" /></webwork:if>
            	        <webwork:else><webwork:property value="pageName" /></webwork:else></a></b>&nbsp;
                </td>
            </webwork:else>
        </webwork:iterator>
        </webwork:if>

    	<td align=right>
            &nbsp;
            <webwork:if test="remoteUser != null">
            <font size=1>
            <webwork:if test="/currentPortalPage/default == false">
            	<webwork:if test="author == true">
                	<b><webwork:text name="'dashboard.configure'"/>:</b>
                	<webwork:if test="/configuring == true"><span style="color: darkgreen; font-weight: bold;"><webwork:text name="'dashboard.configure.on'"/></span> | <a id="configure_off" href="Dashboard.jspa?configuring=false"><webwork:text name="'dashboard.configure.off'"/></a></webwork:if>
                	<webwork:else><a id="configure_on" href="Dashboard.jspa?configuring=true"><webwork:text name="'dashboard.configure.on'"/></a> | <span style="color: darkred; font-weight: bold;"><webwork:text name="'dashboard.configure.off'"/></span></webwork:else>
                	&nbsp;
                </webwork:if>
            </webwork:if>
            <a href="<webwork:url page="SetupPortal.jspa"><webwork:param name="'destination'" value="'dashboard'"/></webwork:url>"><webwork:text name="'portal.configure'" /></a>
            </font>
            </webwork:if>
        </td>
    </tr>
    </table>

    <webwork:if test="/configuring == true">
    <table cellpadding=5 cellspacing=0 border=0 width=100% align=center style="margin: 5px 0 0 0;">
    <tr><td align="right">
        <span style="border: 1px solid #cc9; background: #ffc; padding: 5px;">
            <a href="AddPortlet!default.jspa?destination=dashboard"><img src="<%= request.getContextPath() %>/images/icons/add_16.gif" width=16 height=16 align=absmiddle border=0></a>
            <a href="AddPortlet!default.jspa?destination=dashboard"><webwork:text name="'dashboard.addportlet'"/></a>
        </span>
    </td></tr>
    </table>
    </webwork:if>

    <table cellpadding=5 cellspacing=0 border=0 width=100% align=center>
    <tr>
    <%-- If we have no columns setup, and we are not already configuring, then show a 'please add portlet' box --%>
    <webwork:elseIf test="columns/empty == true || columns == null" >
        <table cellpadding=5 cellspacing=0 border=0 align=left style="margin: 5px 0 0 0;">
        <tr><td align="left" style="border: 1px solid #cc9; background: #ffc; padding: 5px;">
            <span>
                <webwork:text name="'dashboard.noportlets'"/><br>
                <a href="AddPortlet!default.jspa?destination=dashboard"><img src="<%= request.getContextPath() %>/images/icons/add_16.gif" width=16 height=16 align=absmiddle border=0></a>
                <a href="AddPortlet!default.jspa?destination=dashboard"><webwork:text name="'dashboard.addportlet'"/></a>
            </span>
        </td></tr>
        </table>
    </webwork:elseIf>

    <webwork:iterator value="columns" status="'colStatus'">
        <%
            UtilTimerStack.push("PortletColumn");
        %>
        <td valign=top width="<webwork:property value="/columnWidth(@colStatus/count))" />">
            <webwork:if test="@colStatus/last == true">
                <%@ include file="/portlets/dashboard/login.jsp"  %>
            </webwork:if>

            <webwork:iterator value="." status="'rowStatus'">
                <webwork:if test="/configuring == true">
                    <div style="border-width: 1px 1px 0 1px; border-style: solid; border-color: #cc9; padding: 2px; text-align:right; font-size: 10px; <webwork:if test="/selectedPortlet(id) == true">background: #CEE7FF;</webwork:if><webwork:else>background: #eeb;</webwork:else>">
                        <div style="float:left">
                            <a title="View portlet '<webwork:text name="./portlet/name" />' by itself" href="<%= request.getContextPath() %>/secure/RunPortlet.jspa?portletKey=<webwork:url value="portlet/id" />&<webwork:iterator value="properties/keys()"><webwork:property value="." />=<webwork:if test="../objectConfiguration/fieldType(.) == 7"><webwork:property value="../textProperty(.)" /></webwork:if><webwork:else><webwork:property value="../property(.)" /></webwork:else>&</webwork:iterator>"><webwork:text name="./portlet/name" /></a>
                        </div>
                        <webwork:if test="/dashboardPages/size > 1">
                            <select name="moveTo" onchange="location.href='Dashboard.jspa?selectedPortlets=<webwork:property value="id" />&moveToPageId=' + this.options[this.selectedIndex].value; return false;"
                                     id="move_<webwork:property value="id" />">
                                <option><webwork:text name="'dashboard.configure.movetopage'"/></option>
                                <webwork:iterator value="/dashboardPages">
                                    <webwork:if test="/currentPageId != ./id">
                                    <option value="<webwork:url value="./id" />">
                                        <webwork:if test="'dashboard' == ./pageName"><webwork:text name="'common.concepts.dashboard'"/></webwork:if>
                                        <webwork:else><webwork:property value="pageName" /></webwork:else>
                                    </option>
                                    </webwork:if>
                                </webwork:iterator>
                            </select>
                            &nbsp;&nbsp;&nbsp;
                        </webwork:if>

                        <webwork:if test="@colStatus/first == false">
                        <a href="DashboardConfig.jspa?selectedPortlets=<webwork:property value="id" />&dashboardFunction=left" id="left_<webwork:property value="id" />"><img src="<%= request.getContextPath() %>/images/icons/arrow_left_small.gif" width=16 height=16 align=absmiddle border=0 title="<webwork:text name="'dashboard.configure.moveleft'"/>"></a>
                        </webwork:if>
                        <webwork:else>
                        <img src="<%= request.getContextPath() %>/images/icons/arrow_left_faded.gif" width=16 height=16 align=absmiddle border=0>
                        </webwork:else>
                        <webwork:if test="@colStatus/last == false || @colStatus/count == 1">
                        <a href="DashboardConfig.jspa?selectedPortlets=<webwork:property value="id" />&dashboardFunction=right" id="right_<webwork:property value="id" />"><img src="<%= request.getContextPath() %>/images/icons/arrow_right_small.gif" width=16 height=16 align=absmiddle border=0 title="<webwork:text name="'dashboard.configure.moveright'"/>"></a>
                        </webwork:if>
                        <webwork:else>
                        <img src="<%= request.getContextPath() %>/images/icons/arrow_right_faded.gif" width=16 height=16 align=absmiddle border=0>
                        </webwork:else>
                        &nbsp;&nbsp;&nbsp;
                        <webwork:if test="@rowStatus/first == false">
                        <a href="DashboardConfig.jspa?selectedPortlets=<webwork:property value="id" />&dashboardFunction=first" id="first_<webwork:property value="id" />"><img src="<%= request.getContextPath() %>/images/icons/arrow_first.gif" width=16 height=16 align=absmiddle border=0 title="<webwork:text name="'dashboard.configure.movefirst'"/>"></a>
                        </webwork:if>
                        <webwork:else>
                        <img src="<%= request.getContextPath() %>/images/icons/first_faded.gif" width=16 height=16 align=absmiddle border=0>
                        </webwork:else>
                        <webwork:if test="@rowStatus/first == false">
                        <a href="DashboardConfig.jspa?selectedPortlets=<webwork:property value="id" />&dashboardFunction=up" id="up_<webwork:property value="id" />"><img src="<%= request.getContextPath() %>/images/icons/arrow_up_blue.gif" width=16 height=16 align=absmiddle border=0 title="<webwork:text name="'dashboard.configure.moveup'"/>"></a>
                        </webwork:if>
                        <webwork:else>
                        <img src="<%= request.getContextPath() %>/images/icons/arrow_up_faded.gif" width=16 height=16 align=absmiddle border=0>
                        </webwork:else>
                        <webwork:if test="@rowStatus/last == false">
                        <a href="DashboardConfig.jspa?selectedPortlets=<webwork:property value="id" />&dashboardFunction=down" id="down_<webwork:property value="id" />"><img src="<%= request.getContextPath() %>/images/icons/arrow_down_blue.gif" width=16 height=16 align=absmiddle border=0 title="<webwork:text name="'dashboard.configure.movedown'"/>"></a>
                        </webwork:if>
                        <webwork:else>
                        <img src="<%= request.getContextPath() %>/images/icons/arrow_down_faded.gif" width=16 height=16 align=absmiddle border=0>
                        </webwork:else>
                        <webwork:if test="@rowStatus/last == false">
                        <a href="DashboardConfig.jspa?selectedPortlets=<webwork:property value="id" />&dashboardFunction=last" id="last_<webwork:property value="id" />"><img src="<%= request.getContextPath() %>/images/icons/arrow_last.gif" width=16 height=16 align=absmiddle border=0 title="<webwork:text name="'dashboard.configure.movelast'"/>"></a>
                        </webwork:if>
                        <webwork:else>
                        <img src="<%= request.getContextPath() %>/images/icons/last_faded.gif" width=16 height=16 align=absmiddle border=0>
                        </webwork:else>
                        &nbsp;&nbsp;&nbsp;
                        <webwork:if test="/configurationNeeded(.) == true">
                            <a href="SavePortlet!default.jspa?destination=dashboard&portletConfigId=<webwork:property value="id" />" id="config_<webwork:property value="id" />"><img src="<%= request.getContextPath() %>/images/icons/confg_16.gif" width=16 height=16 align=absmiddle border=0 title="<webwork:text name="'common.words.edit'" />"></a>
                        </webwork:if>
                        <webwork:else>
                            <img src="<%= request.getContextPath() %>/images/border/spacer.gif" width=16 height=16 align=absmiddle border=0>
                        </webwork:else>
                        <a href="DashboardConfig.jspa?selectedPortlets=<webwork:property value="id" />&dashboardFunction=copy" id="copy_<webwork:property value="id" />"><img src="<%= request.getContextPath() %>/images/icons/copy_16.gif" width=16 height=16 align=absmiddle border=0 title="<webwork:text name="'common.words.copy'" />"></a>
                        <a href="DashboardConfig.jspa?selectedPortlets=<webwork:property value="id" />&dashboardFunction=delete" id="delete_<webwork:property value="id" />"><img src="<%= request.getContextPath() %>/images/icons/trash_16.gif" width=16 height=16 align=absmiddle border=0 title="<webwork:text name="'common.words.delete'" />"></a>
                    </div>
                    <div style="padding: 5px; border: 1px solid #cc9; background: #ffc;">
                </webwork:if>

                <webwork:if test="portlet != null" >
                    <%
                        final PortletConfiguration portletConfiguration = (PortletConfiguration)CoreActionContext.getValueStack().findValue((String)null);
                        final PortletRenderer portletRenderer = new PortletRenderer();
                     %>

                    <%  String viewHtml = portletRenderer.renderPortlet(request, response, portletConfiguration, true);
                        if (viewHtml != null) //velocity portlet
                        {
                            out.print(viewHtml);
                        }
                        else // jsp portlet
                        {
                            String template = portletConfiguration.getPortlet().getTemplateLocation();
                            UtilTimerStack.push("Running portlet: " + template);
                            try { %>
                                <jsp:include page="<%=template%>" />
                        <% } catch (Throwable t) {
                                System.err.println("Exception rendering portlet "+template);
                                t.printStackTrace();
                        %>
                           <span class="errLabel">Error rendering <%= template %>: <%= t.getMessage() %>. <br>
                           Removing and recreating this portlet may solve the problem.<br>
                           If not please ask your <a href="Administrators.jspa">JIRA adminstrator</a> to send a <a href="admin/jira/JiraSupportRequest!default.jspa">support request</a></span>
                        <% }
                           finally {
                            UtilTimerStack.pop("Running portlet: " + template);
                           }
                        }
                    %>
                </webwork:if>
                <webwork:else>
                    <div class="errorbox">
                        <b><span class="errLabel">WARNING:</span>
                        <webwork:property value="key" /></b><br>
                        <webwork:text name="'dashboard.portlet.disabled'"/>.
                    </div>
                </webwork:else>

                <webwork:if test="/configuring == true">
                    </div>
                </webwork:if>
                <p>
            </webwork:iterator>
            <p>
        </td>
        <%
            UtilTimerStack.pop("PortletColumn");
        %>
    </webwork:iterator>
    </tr>
    </table>
	</body>
</html>
